package com.barbeque.mall.service.wx;

import com.barbeque.mall.bean.param.CommonData;
import com.barbeque.mall.bean.user.MarketAddress;

import com.barbeque.mall.bean.user.MarketAddressExample;
import com.barbeque.mall.bean.user.MarketUser;
import com.barbeque.mall.mapper.MarketAddressMapper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author 苏东宇
 * @since 2022/07/19 16:18
 */
@Service
public class WxAddressServiceImpl implements WxAddressService {


    @Autowired
    MarketAddressMapper marketAddressMapper;

    /**
     * 获取地址列表
     * @return com.barbeque.mall.bean.param.CommonData
     * @author 苏东宇
     * @since 2022/07/19 16:26
     */
    @Override
    public CommonData getAddressList() {


        MarketAddressExample marketAddressExample = new MarketAddressExample();
        MarketAddressExample.Criteria criteria = marketAddressExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        List<MarketAddress> marketAddresses = marketAddressMapper.selectByExample(marketAddressExample);
        PageInfo<MarketAddress> marketAddressPageInfo = new PageInfo<>(marketAddresses);
        return CommonData.data(marketAddressPageInfo);
    }

    /**
     * 创建新的收货地址
 * @param marketAddress
     * @return int
     * @author 苏东宇
     * @since 2022/07/19 16:56
     */
    @Override
    public int createAddress(MarketAddress marketAddress) {
       //目前userID写死
        //整合shrio后使用
        Subject subject = SecurityUtils.getSubject();
        MarketUser marketUser = (MarketUser) subject.getPrincipals().getPrimaryPrincipal();

        marketAddress.setUserId(marketUser.getId());
        marketAddress.setDeleted(false);
        marketAddress.setAddTime(new Date(System.currentTimeMillis()));
        marketAddress.setUpdateTime(new Date(System.currentTimeMillis()));
        int insert = marketAddressMapper.insert(marketAddress);

        return insert;
    }

    /**
     * 根据id查找对应的地址详情
 * @param id
     * @return com.barbeque.mall.bean.user.MarketAddress
     * @author 苏东宇
     * @since 2022/07/19 20:55
     */
    @Override
    public MarketAddress detailAddress(Integer id) {

        MarketAddress marketAddress = marketAddressMapper.selectByPrimaryKey(id);

        return marketAddress;
    }

    /**
     * 更新地址信息
     * @param marketAddress
     * @return int
     * @author 苏东宇
     * @since 2022/07/19 21:19
     */
    @Override
    public int updateAddress(MarketAddress marketAddress) {

        //目前userId写死
        marketAddress.setUserId(1);
        marketAddress.setUpdateTime(new Date(System.currentTimeMillis()));
        marketAddress.setDeleted(false);
        MarketAddressExample marketAddressExample = new MarketAddressExample();
        MarketAddressExample.Criteria criteria = marketAddressExample.createCriteria();
        criteria.andIdEqualTo(marketAddress.getId());

        int i = marketAddressMapper.updateByExample(marketAddress, marketAddressExample);

        return i;
    }

    /**
     * 将所有的is_default设置成false
     * @return int
     * @author 苏东宇
     * @since 2022/07/19 21:48 
     */
    @Override
    public int setDefault() {
        // MarketAddressExample marketAddressExample = new MarketAddressExample();
        // MarketAddressExample.Criteria criteria = marketAddressExample.createCriteria();
        // criteria.andIsDefaultEqualTo(true);
        int i = marketAddressMapper.setDefault();


        return i;
    }

    /**
     * 删除订单
 * @param id
     * @return int
     * @author 苏东宇
     * @since 2022/07/19 22:07
     */
    @Override
    public int deleteAddress(Integer id) {

        int i = marketAddressMapper.deleteByPrimaryKey(id);
        return i;
    }


}
